//
//  MatchingController.m
//  quxiangtao
//
//  Created by HHuajiang on 2018/11/9.
//  Copyright © 2018 shanhaijin. All rights reserved.
//

#import "MatchingController.h"
#import "MatchVModel.h"
#import "MatchViewCell.h"
@interface MatchingController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *orderTable;
@property (nonatomic, strong) NSMutableArray *dataTableSoure;
@property (nonatomic, assign) NSInteger page;

@property (nonatomic, assign) NSInteger pageCount;
@end

@implementation MatchingController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = Colorf2f2f2;
    self.title = @"匹配记录";
    self.page = 1;
    
    NSArray *array = @[@"订单编号",@"匹配时间",@"趣豆"];
    for (int i = 0; i < 3; i ++) {
        UILabel *label = [[UILabel alloc] init];
        label.backgroundColor = WhiteColor;
        label.textAlignment = NSTextAlignmentCenter;
        label.text = array[i];
        label.font = [UIFont systemFontOfSize:12];
        label.textColor = Color999999;
        [self.view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            switch (i) {
                case 0:
                {
                    make.left.mas_equalTo(0);
                    make.width.mas_equalTo(Scale_W(170));
                }
                    break;
                case 1:
                {
                    make.left.mas_equalTo(Scale_W(170));
                    make.width.mas_equalTo(Scale_W(120));
                }
                    break;
                case 2:
                {
                    make.left.mas_equalTo(Scale_W(290));
                    make.width.mas_equalTo(Scale_W(85));
                }
                    break;
                    
                default:
                    break;
            }
            make.top.mas_equalTo(10);
            make.height.mas_equalTo(30);
        }];
    }
    UILabel *lineLabel = [[UILabel alloc] init];
    lineLabel.backgroundColor = Colorf2f2f2;
    [self.view addSubview:lineLabel];
    [lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40);
        make.right.left.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
    
        [self.view addSubview:self.orderTable];
        [self.orderTable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.left.bottom.mas_equalTo(0);
            make.top.mas_equalTo(40.5);
        }];
    [self initRequest];

    WS(weakSelf);
    
    self.orderTable.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        weakSelf.page++;
        [weakSelf initRequest];
    }];
    
}

- (void)initRequest {
    WS(weakSelf);
    NSString *str = [NSString stringWithFormat:@"{\"user_id\" : \"%@\", \"page\" : \"%@\", \"number\" : \"%@\",\"type\" : \"%@\"}", [RSA encryptString:[NSString stringWithFormat:@"%@", [CommonUserInfo sharedInstance].user_id] publicKey:PublicKey], [RSA encryptString:[NSString stringWithFormat:@"%ld",(long)self.page] publicKey:PublicKey], [RSA encryptString:@"10" publicKey:PublicKey],[RSA encryptString:[NSString stringWithFormat:@"%ld",(long)self.indexll] publicKey:PublicKey]];
    [PPNetworkHelper POST:@"user/Welfare/matchinglist" parameters:@{@"json":str} success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue]==1) {
            weakSelf.pageCount = [responseObject[@"count"] intValue];
            NSArray *array = responseObject[@"data"];
            for (int i = 0 ; i < array.count; i ++) {
                MatchVModel *model = [MatchVModel mj_objectWithKeyValues:array[i]];
                [weakSelf.dataTableSoure addObject:model];
            }
            [weakSelf.orderTable reloadData];
        }
        [weakSelf.orderTable.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        [weakSelf.orderTable.mj_footer endRefreshing];
    }];
}

- (UITableView*)orderTable {
    if (!_orderTable) {
        _orderTable = [[UITableView alloc] init];
        _orderTable.backgroundColor = WhiteColor;
        _orderTable.delegate = self;
        _orderTable.dataSource = self;
        [_orderTable registerClass:[MatchViewCell class] forCellReuseIdentifier:@"MatchViewCell"];
        _orderTable.tableFooterView = [[UIView alloc] init];
    }
    return _orderTable;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MatchViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MatchViewCell"];
    MatchVModel *model = self.dataTableSoure[indexPath.row];
    cell.orderNumLabel.text = model.order_id;
    cell.orderTimeLabel.text = model.update_time;
    cell.orderMoneyLabel.text = model.money;
    return  cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataTableSoure.count;
}

- (NSMutableArray *)dataTableSoure {
    if (!_dataTableSoure) {
        _dataTableSoure = [[NSMutableArray alloc] init];
    }
    return _dataTableSoure;
}

@end
